﻿@{
    var isDeleted = "False";
    var db = Database.Open("CvProjectDatabase");
    var selectQuery = "SELECT * FROM Users WHERE IsDeleted = @0";
    var addQuery = "INSERT INTO Users (FirstName, LastName, Username, Password, IsAdmin, IsDeleted) VALUES (@0, @1, @2, @3, @4, @5)";
    var updateQuery = "UPDATE Users SET FirstName = @0, LastName = @1, Username = @2, Password = @3, IsAdmin = @4, IsDeleted = @5 WHERE Username LIKE @6";
    var deleteQuery = "UPDATE Users SET IsDeleted = @0 WHERE Username LIKE @1";
    var selectedData = db.Query(selectQuery, isDeleted );

   
    var grid = new WebGrid(source: selectedData, defaultSort: "FirstName", rowsPerPage:5);
         

    var inputFirstName = "";
    var inputLastName= "";
    var inputUsername = "";
    var inputPassword = "";
    var inputIsAdmin = "False";
    

    

        if(IsPost){
            Validation.RequireField("FirstName", "You have to enter first name!");
            Validation.RequireField("LastName", "You have to enter last name!");
            Validation.RequireField("Username", "You have to enter username!");
            Validation.RequireField("Password", "You have to enter password!");
            var edit = Request.QueryString["edit"].AsBool();
            var id = Request.QueryString["Username"];
            if(Validation.IsValid() && edit){
                

                inputFirstName = Request.Form["FirstName"];
                inputLastName = Request.Form["LastName"];
                inputUsername = Request.Form["Username"];
                if(Request.Form["IsAdmin"] == "on"){
                inputIsAdmin = "True";
                }else{
                    inputIsAdmin = "False";
                }
                db.Execute(updateQuery, inputFirstName, inputLastName, inputUsername, Request.Form["Password"], inputIsAdmin, isDeleted, id);
                Response.Redirect("UserAdministration.cshtml");

            }

            if(Validation.IsValid()){
                inputFirstName = Request.Form["FirstName"];
                inputLastName = Request.Form["LastName"];
                inputUsername = Request.Form["Username"];
                if(Request.Form["IsAdmin"] == "on"){
                inputIsAdmin = "True";
                }else{
                    inputIsAdmin = "False";
                }
                try{
                    db.Execute(addQuery, inputFirstName, inputLastName, inputUsername, Request.Form["Password"], inputIsAdmin, isDeleted);
                    Response.Redirect("UserAdministration.cshtml");
                }catch{
                    Validation.AddFormError("That username already exists, choose another!");
                }
            }

    }


    var delete = Request.QueryString["delete"];
    if(!delete.IsEmpty()){
        isDeleted = "True";
        db.Execute(deleteQuery, isDeleted, delete);
        Response.Redirect("UserAdministration.cshtml");
    }


}

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="res/css/bootstrap.min.css" rel="stylesheet">
    <link href="res/css/tableStyles.css" rel="stylesheet">
    <link href="res/css/userAdministration.css" rel="stylesheet">

   

  </head>
  <body>
   

       <nav class="navbar navbar-default" role="navigation">
            <div class="container-fluid">
                 <ul id="navigacija" class="nav navbar-nav">
                     <li style="margin-right: 30px"><a href="Index.cshtml"><b style="font-size: 1.7em">CV Database</b></a></li>
                     <li style="font-size: 1.2em"><a href="UserAdministration.cshtml">User Administration</a></li>
                     <li style="padding-top: 15px"> | </li>
                   
                    
                        <li class="dropdown" style="font-size: 1.2em">
                        
                            
                            <li style="font-size: 1.2em"><a href="Login.cshtml?izlogujSe=true">Logout</a></li>
                        </ul>
                    </li>
                    
                 </ul>
            </div>
        </nav>
      <div class="divAll">
      @{
          
          
      
     <div id="divRight">
      @grid.GetHtml(
                    tableStyle: "grid",
                    headerStyle: "head",
                    alternatingRowStyle: "alt",
                    columns: grid.Columns(
                    grid.Column("FirstName"),
                    grid.Column("LastName"),
                    grid.Column("Username"),
                    grid.Column("Password"),
                    grid.Column("IsAdmin"),
                    grid.Column(format: @<a href="~/UserAdministration?edit=true&Username=@item.Username&FirstName=@item.FirstName&LastName=@item.LastName&IsAdmin=@item.IsAdmin">Edit</a>),
                    grid.Column(format: @<a href="~/UserAdministration?delete=@item.Username" onclick ="return confirm('Are you sure you want to delete this user?')">Delete</a>)
                )
            )
      </div>
    
      }
          
      <div id="divLeft">
         
      <form method="post">
          @Html.ValidationSummary()
            <fieldset>
              <legend>User info</legend>               
              <p><label for="FirstName" class="compLeft">First name:</label>
                 <input class="compRight"type="text" name="FirstName" value="@Request.QueryString["FirstName"]"  />
                  @Html.ValidationMessage("FirstName")
              </p>

              <p><label for="LastName" class="compLeft">Last name:</label>
                 <input class="compRight" type="text" name="LastName" value="@Request.QueryString["LastName"]"  />
                  @Html.ValidationMessage("LastName")
              </p>

              <p><label for="Username" class="compLeft">Username:</label>
                 <input class="compRight" type="text" name="Username" value="@Request.QueryString["Username"]"  />
                  @Html.ValidationMessage("Username")
              </p>

              <p><label for="Password" class="compLeft">Password:</label>
                 <input class="compRight" type="password" name="Password"  />
                  @Html.ValidationMessage("Password")
              </p>

              <p><label for="uloga" class="compLeft">Is admin?</label>
                 <input type="checkbox" name="IsAdmin"/>
              </p>

              <p><input class="compBot"type="submit" name="buttonSubmit" value="Save" /></p>
            </fieldset>
          </form>
             </div>
             </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="res/js/bootstrap.min.js"></script>
  </body>
</html>